<?php

namespace App\Console\Commands\Manual;

use App\Models\Tools\LaifeiPublish;
use Illuminate\Console\Command;

class LaifeiRefresh extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:laifei_refresh';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新提交来飞静态资源到阿里云';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // OSS上传
        $laifeiPublish = new LaifeiPublish();
        // 删除已有文件
        $remove = $laifeiPublish->removeFiles();
        if($remove){
            $this->comment('删除OSS文件成功,正在上传本地文件...');
        } else {
            $this->error("删除OSS文件失败\n请在OSS对应的bucket删除除history之外的所有文件后，手动上传/dist文件夹内的所有文件\n您可前往运营端查看日志");
            return false;
        }

        $laifeiPublish->list_file($laifeiPublish->url);
        $bar = $this->output->createProgressBar(count($laifeiPublish->files));
        $bar->start();
        $result = $laifeiPublish->uploadFiles($bar);
        $bar->finish();

        if($result){
            $this->info("\n\n上传成功！！！\n");
        } else {
            $this->error("\n上传失败，请在OSS对应的bucket手动上传/dist文件夹内的所有文件\n您可前往运营端查看日志\n");
            return false;
        }
        return true;
    }
}
